const express = require("express");
const { createServer } = require("http");
const { Server } = require("socket.io");
const { SOCKET_SERVER_PORT } = require('./constant');
const { onListener } = require('./socket/onListener');
const { getUrlParams } = require('./utils/getUrlParams');
const { USERID_KEY, ROOMID_KEY } = require('./utils/clientQueryKey');

const app = express();
const httpServer = createServer(app);
const io = new Server(httpServer, {cors: true});

io.on("connection", (socket) => {
  const clientUrl = socket.client.request.url;
  const [ userId, roomId ] = [ getUrlParams(clientUrl, USERID_KEY), getUrlParams(clientUrl, ROOMID_KEY) ];
  console.log(userId, roomId);

  onListener(socket)
});



httpServer.listen(SOCKET_SERVER_PORT);